package com.bmf.admin.utils;


import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.ZipUtil;
import com.bmf.core.constant.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Slf4j
public class FileUtils {
    /**
     * 多图片压缩zip
     *
     * @param srcFiles 图片名称
     * @param zipFile  文件路径
     */
    public static void zipFiles(List<String> srcFiles, File zipFile) {

        // 判断压缩后的文件存在不，不存在则创建
        if (!zipFile.exists()) {
            try {
                zipFile.createNewFile();
            } catch (IOException e) {
                log.error(ExceptionUtil.stacktraceToString(e));
                ResultCodeEnum.SERVER_ERROR.assertFail();
            }
        }
        FileInputStream inputStream = null;
        try (FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
             ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream)
        ) {
            // 创建 ZipEntry 对象
            ZipEntry zipEntry;
            // 遍历源文件数组
            for (String file : srcFiles) {
                // 将源文件数组中的当前文件读入 FileInputStream 流中
                String fileName = file;
                String localFileDir = "D:\\file\\video";
                fileName = URLDecoder.decode(fileName, "UTF-8");
                //获取文件输入流  localFileDir是服务端存储文件的路径
                File files = new File(localFileDir + "/" + fileName);
                inputStream = new FileInputStream(files);
                // 文件后缀名称
                // 实例化 ZipEntry 对象，源文件数组中的当前文件
                zipEntry = new ZipEntry(fileName);
                zipOutputStream.putNextEntry(zipEntry);
                // 该变量记录每次真正读的字节个数
                int len;
                // 定义每次读取的字节数组
                byte[] buffer = new byte[4096];
                while ((len = inputStream.read(buffer)) > 0) {
                    zipOutputStream.write(buffer, 0, len);
                }
            }
            zipOutputStream.closeEntry();
        } catch (IOException e) {
            log.error(ExceptionUtil.stacktraceToString(e));
            ResultCodeEnum.SERVER_ERROR.assertFail();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                log.error(ExceptionUtil.stacktraceToString(e));
                ResultCodeEnum.SERVER_ERROR.assertFail();
            }
        }
    }
    /**
     * 文件压缩zip 并返回 下载地址
     *
     * @param fileDir   需要压缩的文件目录
     * @param delOldDir 删除旧文件目录
     * @return {@link String}
     */
    public static String zipFile(String fileDir, boolean delOldDir) {

        String zipFile = fileDir + ".zip";
        File file = new File(fileDir);
        ZipUtil.zip(file, StandardCharsets.UTF_8);
        if (delOldDir) {
            deleteDir(file);
        }
        return zipFile;
    }
    /**
     * 删除目录
     *
     * @param directory 目录
     */
    public static void deleteDir(File directory) {
        //获取目录下所有文件和目录
        File files[] = directory.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                deleteDir(file);
            } else {
                file.delete();
            }
        }
        //最终把该目录也删除
        directory.delete();
    }
}
